home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / pcl4c40.zip / CRC.C < prev    next >
Text File  |  1993-10-06  |  737b  |  37 lines

  1. #include <stdio.h>
  2. #include "crc.h"
  3.  
  4. #define POLY 0x1021
  5.  
  6. static unsigned short CRCtable[256];
  7.  
  8. /* initialize CRC table */
  9.  
  10. void InitCRC(void)
  11. {int i;
  12.  for(i=0;i<256;i++) CRCtable[i] = CalcTable(i,POLY,0);
  13. }
  14.  
  15. /* calculate CRC table entry */
  16.  
  17. unsigned short CalcTable(
  18.    unsigned short data,
  19.    unsigned short genpoly,
  20.    unsigned short accum)
  21. {static int i;
  22.  data <<= 8;
  23.  for(i=8;i>0;i--)
  24.          {
  25.           if((data^accum) & 0x8000) accum = (accum << 1) ^ genpoly;
  26.           else accum <<= 1;
  27.           data <<= 1;
  28.          }
  29.  return(accum);
  30. }
  31.  
  32. /* compute updated CRC */
  33.  
  34. unsigned short UpdateCRC(unsigned short crc, unsigned char byte)
  35. {
  36.  return( (crc << 8) ^ CRCtable[ (crc >> 8) ^ byte ] );
  37. }